Page({
  //异步请求 越界复制

  data: {
    datalist:[],
    imgurl: "cloud://user-8gd9jrjnd1e10d35.7573-user-8gd9jrjnd1e10d35-1321481870/qqq.png",
    mediaurl:"cloud://user-8gd9jrjnd1e10d35.7573-user-8gd9jrjnd1e10d35-1321481870/1699885422495.mp4"
  },
  getData(){
    let that=this
    wx.cloud.database().collection("userList").get({
      success(res){
        console.log("请求成功",res)
        that.setData({
          datalist:res.data
        })
      },
      fail(res){
        console.log("请求失败",res)
      }
    })
  },

  upload() {
    let that = this;
    console.log("正在上传");
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log("选择成功", res);
        const tempFilePath = res.tempFilePaths[0];
        that.uploadImg(tempFilePath);
      },
    });
  },

  uploadImg(fileUrl) {
    wx.cloud.uploadFile({
      cloudPath: new Date().getTime() + '.png',
      filePath: fileUrl,
      success: res => {
        console.log("上传成功", res);
        this.setData({
          imgurl: res.fileID
        });
      },
      fail: error => {
        console.error("上传失败", error);
      }
    });
  },

  uploadVideo() {
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log("选择视频成功", tempFilePath);
        wx.cloud.uploadFile({
          cloudPath: new Date().getTime() + '.mp4',
          filePath: tempFilePath,
          success: res => {
            console.log("上传成功", res);
            this.setData({
              imgurl: res.fileID
            });
          },
          fail: error => {
            console.error("上传失败", error);
          }
        });
      }
    });
  },
  uploadExcel(){
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePath = res.tempFiles[0].path; // 修改这里
        wx.cloud.uploadFile({
          cloudPath: new Date().getTime() + '.xls',
          filePath: tempFilePath,
          success: res => {
            console.log("上传Excel成功", res);
            that.setData({
              fileurl: res.fileID
            });
          },
          fail: error => {
            console.error("上传Excel失败", error);
          }
        });
      }
    })
  },

   openExcel(){
   //下载并打开文件
   wx.cloud.downloadFile({
     fileID:'cloud://user-8gd9jrjnd1e10d35.7573-user-8gd9jrjnd1e10d35-1321481870/1699886797908.xls',
     success:res=>{
       wx.openDocument({
         filePath: res.tempFilePath,
         success:function(res){
           console.log('打开文件成功')
         }
       })
     },
     fail:err=>{
       //none
     }

   }) 
   
  }
  

});
